package cn.openea.perpartner.timeline.web.service.impl;

import cn.openea.perpartner.timeline.web.entity.ArcData;
import cn.openea.perpartner.timeline.web.entity.ArcTicket;
import cn.openea.perpartner.timeline.web.entity.dto.SearchDto;
import cn.openea.perpartner.timeline.web.entity.dto.UserGroupDto;
import cn.openea.perpartner.timeline.web.mapper.ArcDataMapper;
import cn.openea.perpartner.timeline.web.mapper.ArcTicketMapper;
import cn.openea.perpartner.timeline.web.service.ArcDataService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (ArcData)表服务实现类
 *
 * @author Weird077
 * @since 2022-12-11 22:16:28
 */
@Service("arcDataService")
public class ArcDataServiceImpl extends ServiceImpl<ArcDataMapper, ArcData> implements ArcDataService {
    @Resource
    private ArcTicketMapper ticketMapper;

    @Override
    public List<ArcData> selectAll(SearchDto dto) {
        ArcTicket ticket = ticketMapper.selectById(dto.getTicketId());
        LambdaQueryWrapper<ArcData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArcData::getTicketId, dto.getTicketId());
        if (!dto.getUserId().equals(ticket.getUserId()) && dto.getUserList().isEmpty()) {
            dto.getUserList().add(dto.getUserId());
            dto.getUserList().add(ticket.getUserId());
        }
        if (!dto.getUserList().isEmpty()) {
            wrapper.in(ArcData::getUserId, dto.getUserList());
        }
        wrapper.orderByAsc(ArcData::getCreateTime);
        return list(wrapper);
    }

    @Override
    public List<UserGroupDto> selectUserGroup(SearchDto dto) {
        ArcTicket ticket = ticketMapper.selectById(dto.getTicketId());
        List<UserGroupDto> userList = baseMapper.selectUserGroup(dto.getTicketId());
        if (dto.getUserId().equals(ticket.getUserId())) {
            return userList;
        }
        return userList.stream().filter(user ->
                        user.getUserId().equals(dto.getUserId())
                                || user.getUserId().equals(ticket.getUserId()))
                .collect(Collectors.toList());

    }
}

